<?php
/**
 * TODO: Extend from base-model
 */
class FileModel {
    
    private $path = "Public/Files/" ;

    public function setPath($path) {
        $this->path = $path ;
    }
    public function getPath() {
        return $this->path ;
    }
    /**
     * Get all files from the path specified above
     * 
     * @return array $retArray Save all the filenames in the array
     *
     * TODO: Error-handling
     */   
    public function getFiles() {

        $retArray = array() ;
        if(is_dir($this->path)) {
            if($dh = opendir($this->path)) {
                while(($file = readdir($dh)) !== false) {
                    // Don't show hidden files or parent and current directory.
                    if($file[0] !== ".") {
                        $retArray[] = $file ;
                    }
                }
            }
        } else {
            echo "is not a directory" ;
            // not a direcotory
            return false ;
        }

        return $retArray ; 


    }
    /**
     * Function to save a file to the directory
     *
     * @param file $file File to save
     *
     * @return boolean true or false depending on the outcome
     */
    public function saveFile($file) {
        if($file['error'] == 0) {
            if (file_exists($this->path.$file['name'])) {
                return false ;
            } else {
                if (file_exists($file['tmp_name'])) {
                    move_uploaded_file($file['tmp_name'], $this->path.$file['name']) ;
                } else {
                    return false ;
                }
            }
        } else {
            return false ;
        }
        
        return true ;

    }

    public function removeFile($file) {
        // To be implemented?
    }

}
